package com.css.zfzx.sjcj.modules.qhdatatemplate.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.qhdatatemplate.repository.entity.QhDataTemplateEntity;
import com.css.zfzx.sjcj.modules.qhdatatemplate.service.QhDataTemplateService;
import com.css.zfzx.sjcj.modules.qhdatatemplate.viewobjects.QhDataTemplateQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author tao
 * @date 2020-11-19
 */
@Slf4j
@RestController
@RequestMapping("/qh/qhDataTemplates")
public class QhDataTemplateController {
    @Autowired
    private QhDataTemplateService qhDataTemplateService;

    @GetMapping("/queryQhDataTemplates")
    public RestResponse queryQhDataTemplates(HttpServletRequest request, QhDataTemplateQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = qhDataTemplateService.queryQhDataTemplates(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getQhDataTemplate(@PathVariable String id) {
        RestResponse response = null;
        try{
            QhDataTemplateEntity qhDataTemplate = qhDataTemplateService.getQhDataTemplate(id);
            response = RestResponse.succeed(qhDataTemplate);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveQhDataTemplate(@RequestBody QhDataTemplateEntity qhDataTemplate) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhDataTemplateService.saveQhDataTemplate(qhDataTemplate);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateQhDataTemplate(@RequestBody QhDataTemplateEntity qhDataTemplate)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhDataTemplateService.updateQhDataTemplate(qhDataTemplate);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @DeleteMapping
    public RestResponse deleteQhDataTemplates(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhDataTemplateService.deleteQhDataTemplates(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/upload")
    public RestResponse upload(@RequestParam("file") MultipartFile file) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            qhDataTemplateService.upload(file);
            json.put("message", "上传成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "上传失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/download/{name}")
    public RestResponse exportExcel(@PathVariable String name, HttpServletResponse response) {
        RestResponse restResponse = null;
        JSONObject json = new JSONObject();
        try {
            qhDataTemplateService.download(name, response);
            json.put("message", "下载成功!");
            restResponse = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMsg = "下载失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

}